Completed
Push — master ( b895c6...fa6d7b )
by Rain
03:32
created

PromisesUserPopulator.folderResponseParseRec   D

Complexity

Conditions 15
Paths 113

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
c 1
b 0
f 0
nc 113
dl 0
loc 77
rs 4.956
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like PromisesUserPopulator.folderResponseParseRec often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
2
var
3
	_ = require('_'),
4
5
	Consts = require('Common/Consts'),
6
	Enums = require('Common/Enums'),
7
	Utils = require('Common/Utils'),
8
	Cache = require('Common/Cache'),
9
10
	AppStore = require('Stores/User/App'),
11
	FolderStore = require('Stores/User/Folder'),
12
13
	Settings = require('Storage/Settings'),
14
	Local = require('Storage/Client'),
15
16
	FolderModel = require('Model/Folder').default,
17
18
	AbstractBasicPromises = require('Promises/AbstractBasic');
19
20
/**
21
 * @constructor
22
 */
23
function PromisesUserPopulator()
24
{
25
	AbstractBasicPromises.call(this);
26
}
27
28
_.extend(PromisesUserPopulator.prototype, AbstractBasicPromises.prototype);
29
30
/**
31
 * @param {string} sFullNameHash
32
 * @param {Array?} expandedFolders
33
 * @returns {boolean}
34
 */
35
PromisesUserPopulator.prototype.isFolderExpanded = function(sFullNameHash, expandedFolders)
36
{
37
	return expandedFolders && Utils.isArray(expandedFolders) && -1 !== _.indexOf(expandedFolders, sFullNameHash);
38
};
39
40
/**
41
 * @param {string} sFolderFullNameRaw
42
 * @returns {string}
43
 */
44
PromisesUserPopulator.prototype.normalizeFolder = function(sFolderFullNameRaw)
45
{
46
	return ('' === sFolderFullNameRaw || Consts.UNUSED_OPTION_VALUE === sFolderFullNameRaw ||
47
		null !== Cache.getFolderFromCacheList(sFolderFullNameRaw)) ? sFolderFullNameRaw : '';
48
};
49
50
/**
51
 * @param {string} sNamespace
52
 * @param {Array} aFolders
53
 * @param {Array?} expandedFolders
54
 * @returns {Array}
55
 */
56
PromisesUserPopulator.prototype.folderResponseParseRec = function(sNamespace, aFolders, expandedFolders)
57
{
58
	var
59
		self = this,
60
		iIndex = 0,
61
		iLen = 0,
62
		oFolder = null,
0 ignored issues
show
Unused Code introduced by
The assignment to oFolder seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
63
		oCacheFolder = null,
0 ignored issues
show
Unused Code introduced by
The assignment to oCacheFolder seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
64
		bDisplaySpecSetting = FolderStore.displaySpecSetting(),
65
		sFolderFullNameRaw = '',
66
		aSubFolders = [],
0 ignored issues
show
Unused Code introduced by
The assignment to variable aSubFolders seems to be never used. Consider removing it.
Loading history...
67
		aList = [];
68
69
	for (iIndex = 0, iLen = aFolders.length; iIndex < iLen; iIndex++)
70
	{
71
		oFolder = aFolders[iIndex];
72
		if (oFolder)
73
		{
74
			sFolderFullNameRaw = oFolder.FullNameRaw;
75
76
			oCacheFolder = Cache.getFolderFromCacheList(sFolderFullNameRaw);
77
			if (!oCacheFolder)
78
			{
79
				oCacheFolder = FolderModel.newInstanceFromJson(oFolder);
80
				if (oCacheFolder)
81
				{
82
					Cache.setFolderToCacheList(sFolderFullNameRaw, oCacheFolder);
83
					Cache.setFolderFullNameRaw(oCacheFolder.fullNameHash, sFolderFullNameRaw, oCacheFolder);
84
				}
85
			}
86
87
			if (oCacheFolder)
88
			{
89
				if (bDisplaySpecSetting)
90
				{
91
					oCacheFolder.checkable(!!oFolder.Checkable);
92
				}
93
				else
94
				{
95
					oCacheFolder.checkable(true);
96
				}
97
98
				oCacheFolder.collapsed(!self.isFolderExpanded(oCacheFolder.fullNameHash, expandedFolders));
99
100
				if (oFolder.Extended)
101
				{
102
					if (oFolder.Extended.Hash)
103
					{
104
						Cache.setFolderHash(oCacheFolder.fullNameRaw, oFolder.Extended.Hash);
105
					}
106
107
					if (Utils.isNormal(oFolder.Extended.MessageCount))
108
					{
109
						oCacheFolder.messageCountAll(oFolder.Extended.MessageCount);
110
					}
111
112
					if (Utils.isNormal(oFolder.Extended.MessageUnseenCount))
113
					{
114
						oCacheFolder.messageCountUnread(oFolder.Extended.MessageUnseenCount);
115
					}
116
				}
117
118
				aSubFolders = oFolder.SubFolders;
119
				if (aSubFolders && 'Collection/FolderCollection' === aSubFolders['@Object'] &&
120
					aSubFolders['@Collection'] && Utils.isArray(aSubFolders['@Collection']))
121
				{
122
					oCacheFolder.subFolders(
123
						this.folderResponseParseRec(sNamespace, aSubFolders['@Collection'], expandedFolders));
124
				}
125
126
				aList.push(oCacheFolder);
127
			}
128
		}
129
	}
130
131
	return aList;
132
};
133
134
PromisesUserPopulator.prototype.foldersList = function(oData)
135
{
136
	if (oData && 'Collection/FolderCollection' === oData['@Object'] &&
137
		oData['@Collection'] && Utils.isArray(oData['@Collection']))
138
	{
139
		var
140
			folderList = [],
0 ignored issues
show
Unused Code introduced by
The assignment to variable folderList seems to be never used. Consider removing it.
Loading history...
141
			expandedFolders = Local.get(Enums.ClientSideKeyName.ExpandedFolders),
142
			iLimit = Utils.pInt(Settings.appSettingsGet('folderSpecLimit')),
143
			iC = Utils.pInt(oData.CountRec);
144
145
		iLimit = 100 < iLimit ? 100 : (10 > iLimit ? 10 : iLimit);
146
147
		FolderStore.displaySpecSetting(0 >= iC || iLimit < iC);
148
		folderList = this.folderResponseParseRec(Utils.isUnd(oData.Namespace) ? '' : oData.Namespace, oData['@Collection'], expandedFolders);
149
		FolderStore.folderList(folderList); // @todo optimization required
150
	}
151
};
152
153
PromisesUserPopulator.prototype.foldersAdditionalParameters = function(oData)
154
{
155
	if (oData && oData && 'Collection/FolderCollection' === oData['@Object'] &&
156
		oData['@Collection'] && Utils.isArray(oData['@Collection']))
157
	{
158
		if (!Utils.isUnd(oData.Namespace))
159
		{
160
			FolderStore.namespace = oData.Namespace;
161
		}
162
163
		AppStore.threadsAllowed(!!Settings.appSettingsGet('useImapThread') && oData.IsThreadsSupported && true);
164
165
		FolderStore.folderList.optimized(!!oData.Optimized);
166
167
		var bUpdate = false;
168
169
		if (oData.SystemFolders && '' === '' +
170
			Settings.settingsGet('SentFolder') +
171
			Settings.settingsGet('DraftFolder') +
172
			Settings.settingsGet('SpamFolder') +
173
			Settings.settingsGet('TrashFolder') +
174
			Settings.settingsGet('ArchiveFolder') +
175
			Settings.settingsGet('NullFolder'))
176
		{
177
			Settings.settingsSet('SentFolder', oData.SystemFolders[Enums.ServerFolderType.SENT] || null);
178
			Settings.settingsSet('DraftFolder', oData.SystemFolders[Enums.ServerFolderType.DRAFTS] || null);
179
			Settings.settingsSet('SpamFolder', oData.SystemFolders[Enums.ServerFolderType.JUNK] || null);
180
			Settings.settingsSet('TrashFolder', oData.SystemFolders[Enums.ServerFolderType.TRASH] || null);
181
			Settings.settingsSet('ArchiveFolder', oData.SystemFolders[Enums.ServerFolderType.ALL] || null);
182
183
			bUpdate = true;
184
		}
185
186
		FolderStore.sentFolder(this.normalizeFolder(Settings.settingsGet('SentFolder')));
187
		FolderStore.draftFolder(this.normalizeFolder(Settings.settingsGet('DraftFolder')));
188
		FolderStore.spamFolder(this.normalizeFolder(Settings.settingsGet('SpamFolder')));
189
		FolderStore.trashFolder(this.normalizeFolder(Settings.settingsGet('TrashFolder')));
190
		FolderStore.archiveFolder(this.normalizeFolder(Settings.settingsGet('ArchiveFolder')));
191
192
		if (bUpdate)
193
		{
194
			require('Remote/User/Ajax').saveSystemFolders(Utils.noop, {
195
				SentFolder: FolderStore.sentFolder(),
196
				DraftFolder: FolderStore.draftFolder(),
197
				SpamFolder: FolderStore.spamFolder(),
198
				TrashFolder: FolderStore.trashFolder(),
199
				ArchiveFolder: FolderStore.archiveFolder(),
200
				NullFolder: 'NullFolder'
201
			});
202
		}
203
204
		Local.set(Enums.ClientSideKeyName.FoldersLashHash, oData.FoldersHash);
205
	}
206
};
207
208
module.exports = new PromisesUserPopulator();
209